package com.chookss.home;

import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.fragment.app.Fragment;

import com.chookss.R;
import com.chookss.home.document.DocumentBaseFragment;
import com.chookss.home.entity.ContactEntity;
import com.chookss.home.gallery.GalleryBaseFragment;
import com.chookss.home.interview.InterviewBaseFragment;
import com.chookss.home.knowledge.KnowledgeBaseFragment;
import com.chookss.home.logContacts.ContactsBaseFragment;
import com.chookss.home.logContacts.LookLogBaseFragment;
import com.chookss.home.myTest.TestBaseFragment;
import com.chookss.home.other.NoticeBaseFragment;
import com.chookss.home.rank.RankBaseFragment;
import com.chookss.home.studyPlan.StudyPlanBaseFragment;
import com.chookss.home.tuwen.TuwenBaseFragment;
import com.chookss.tiku.TikuFragments;
import com.chookss.tools.MyEvent;
import com.chookss.tools.NavigationTools;
import com.lvgroup.hospital.base.BaseAct;

import org.greenrobot.eventbus.EventBus;
import org.jetbrains.annotations.Nullable;

import java.util.List;

import butterknife.BindView;

public class NavigationBaseActivity extends BaseAct {

    @BindView(R.id.fragment_container)
    FrameLayout fragment_container;
    @BindView(R.id.llTop)
    LinearLayout llTop;
    private Context mContext;
    //                01  公告管理 02  我的日志 03  资料库 04  学习计划
//                05  我的考试 06  一键面试 07  通讯录 08  个人能力
    private int type = 0;//0题库1公告2日志3资料库4学习计划5我的考试6一键面试7通讯录8个人能力15溯源16发布图文17图文24知识库
    private Fragment fragment;
    private LookLogBaseFragment lookLogBaseFragment;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_navigation_base);
        mContext = this;
        initState();
    }

    @Override
    public void initData() {
        if (llTop == null) {
            finish();
        }
        try {
            llTop.setBackgroundColor(getResources().getColor(R.color.white));
            type = getIntent().getIntExtra("type", 0);

            if (type == 1) {
                fragment = new NoticeBaseFragment(1);
            } else if (type == 2) {
                lookLogBaseFragment = new LookLogBaseFragment(1);
            } else if (type == 3) {
                fragment = new DocumentBaseFragment(1);
            } else if (type == 4) {
                fragment = new StudyPlanBaseFragment(1);
            } else if (type == 5) {
                fragment = new TestBaseFragment(1);
            } else if (type == 6) {
                fragment = new InterviewBaseFragment(1);
                NavigationTools.setWaterMark(this, "16");
            } else if (type == 7) {
                fragment = new ContactsBaseFragment(1);
            } else if (type == 15) {
                fragment = new RankBaseFragment(1);
            } else if (type == 17) {
                fragment = new TuwenBaseFragment(1);
            } else if (type == 24) {
                fragment = new KnowledgeBaseFragment(1);
            } else if (type == 26) {
                fragment = new GalleryBaseFragment(1, getIntent().getIntExtra("roleType", 1));
            } else {
                fragment = new TikuFragments(1);
            }
            if (type == 2) {
                getSupportFragmentManager()    //
                        .beginTransaction()
                        .add(R.id.fragment_container, lookLogBaseFragment)
                        .commit();
            } else {
                getSupportFragmentManager()    //
                        .beginTransaction()
                        .add(R.id.fragment_container, fragment)
                        .commit();
            }
        } catch (Resources.NotFoundException e) {
            finish();
            e.printStackTrace();
        } catch (Exception e) {
            finish();
        }
        if (type == 6) {
            NavigationTools.setWaterMark(this, "16");
        }
    }

    @Override
    protected void onRestart() {
        super.onRestart();
        if (type == 1) {
            EventBus.getDefault().post(new MyEvent("NoticeBaseFragment"));
        } else if (type == 3) {
            EventBus.getDefault().post(new MyEvent("RefreshDocumentFragment"));
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, @androidx.annotation.Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == 1) {
            if (data != null) {
                if (type == 2 && lookLogBaseFragment != null) {
                    lookLogBaseFragment.setOnActivityResult(data.getStringExtra("startTime"), data.getStringExtra("endTime"), data.getStringExtra("reportType"), (List<ContactEntity>) data.getSerializableExtra("data"));
                }

            }
        }
    }
}
